package frmMantenimiento;

import cls.conexion;
import com.sun.glass.events.KeyEvent;
import frm.frmPrincipal;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class frmArticulo extends javax.swing.JFrame {

    static conexion CP; // conexion con base de datos

    // metodo de inicio
    public frmArticulo() throws ClassNotFoundException, SQLException {
        initComponents();
        setLocationRelativeTo(null);

        CP = new conexion("proyecto2", "usrUTN", "12345");
        noVer();
        verImpuestos();
        verFamilias();
        verMarcas();
        verdatos();
    }

    private void verMarcas() {
        this.cbxMarca.removeAllItems();
        try {
            ResultSet datos = frmArticulo.CP.select("*", "\"schUTN\".marca_articulo",
                    "activo= " + "'S'" + "");
            while (datos.next()) {

                this.cbxMarca.addItem(datos.getString(2));

            }
        } catch (Exception e) {
        }

    }

    private void verImpuestos() {
        this.cbxImpuesto.removeAllItems();
        try {
            ResultSet datos = frmArticulo.CP.select("*", "\"schUTN\".impuesto",
                    "activo= " + "'S'" + "");
            while (datos.next()) {

                this.cbxImpuesto.addItem(datos.getString(2));

            }
        } catch (Exception e) {
        }

    }

    private void verFamilias() {
        this.cbxFamilias.removeAllItems();
        try {
            ResultSet datos = frmArticulo.CP.select("*", "\"schUTN\".familia_articulo",
                    "activo= " + "'S'" + "");
            while (datos.next()) {

                this.cbxFamilias.addItem(datos.getString(2));

            }
        } catch (Exception e) {
        }

    }

    // metodo para mostras los datos de la base de datos en la tabla
    private void verdatos() {
        DefaultTableModel modelo = (DefaultTableModel) tblarticulos.getModel();
        Object[] fila = new Object[11];
        modelo.setRowCount(0);
        try {
            ResultSet datos = frmArticulo.CP.select("*", "\"schUTN\".articulo", "");
            while (datos.next()) {
                fila[0] = datos.getString(1);

                
                fila[3] = datos.getString(4);
                fila[4] = datos.getString(5);
                fila[5] = datos.getString(9);
                fila[6] = datos.getString(6);
                fila[7] = datos.getString(7);
                fila[8] = datos.getString(8);
                fila[9] = datos.getString(9);
                fila[10] = datos.getString(10);

                ResultSet datos2 = CP.select("descripcion", "\"schUTN\".familia_articulo",
                        "cod_familia =" + "'" + datos.getString(2) + "'");

                while (datos2.next()) {

                    fila[1] = datos2.getString(1);

                }
                
                ResultSet datos3 = CP.select("descripcion", "\"schUTN\".marca_articulo",
                        "cod_marca =" + "'" + datos.getString(3) + "'");

                while (datos3.next()) {

                    fila[2] = datos3.getString(1);

                }
                
                

                modelo.addRow(fila);
            }
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString());
        }
    }

    private void limpiarRd() {
        rdNo.setSelected(false);
        rdSi.setSelected(false);
        rdNo.setEnabled(true);
        rdSi.setEnabled(true);
    }

    // metodo no ver
    private void noVer() {
        
        jLabel1.setVisible(false);
        jLabel8.setVisible(false);
        jLabel9.setVisible(false);
        jLabel10.setVisible(false);
        jLabel11.setVisible(false);
        jLabel12.setVisible(false);
        jLabel13.setVisible(false);
        jLabel14.setVisible(false);
        jLabel4.setVisible(false);
        jlaexistencia.setVisible(false);
        txtExistencia.setVisible(false);
        txtcodarti.setVisible(false);
        cbxFamilias.setVisible(false);
        cbxImpuesto.setVisible(false);
        cbxMarca.setVisible(false);
        txtcosto.setVisible(false);
        txtdescripcion.setVisible(false);
        txtUtilidad.setVisible(false);
        txtpresinim.setVisible(false);
        btnfinalizarinsertar.setVisible(false);
        btnfinalizarmodifi.setVisible(false);
        rdNo.setVisible(false);
        rdSi.setVisible(false);
    }

    // metodo ver botones
    public void ver() {
        jLabel1.setVisible(true);
        jLabel8.setVisible(true);
        jLabel9.setVisible(true);
        jLabel10.setVisible(true);
        jLabel11.setVisible(true);
        jLabel12.setVisible(true);
        jLabel13.setVisible(true);
        jLabel14.setVisible(true);
        jLabel4.setVisible(true);
        jlaexistencia.setVisible(true);
        txtExistencia.setVisible(true);
        txtcodarti.setVisible(true);
        cbxFamilias.setVisible(true);
        cbxImpuesto.setVisible(true);
        cbxMarca.setVisible(true);
        txtcosto.setVisible(true);
        txtdescripcion.setVisible(true);
        txtUtilidad.setVisible(true);
        txtpresinim.setVisible(true);
        btnfinalizarinsertar.setVisible(true);
        btnfinalizarmodifi.setVisible(true);
        rdSi.setVisible(true);
        rdNo.setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblarticulos = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        txtcodarti = new javax.swing.JTextField();
        txtdescripcion = new javax.swing.JTextField();
        txtpresinim = new javax.swing.JTextField();
        txtcosto = new javax.swing.JTextField();
        btninsertar = new javax.swing.JButton();
        btnmodificar = new javax.swing.JButton();
        btneliminar = new javax.swing.JButton();
        btnfinalizarmodifi = new javax.swing.JButton();
        btnfinalizarinsertar = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        rdSi = new javax.swing.JRadioButton();
        rdNo = new javax.swing.JRadioButton();
        cbxFamilias = new javax.swing.JComboBox();
        cbxMarca = new javax.swing.JComboBox();
        cbxImpuesto = new javax.swing.JComboBox();
        txtUtilidad = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jlaexistencia = new javax.swing.JLabel();
        txtExistencia = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("ARTICULOS");
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        tblarticulos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cod Articulo", "Cod Familia", "Cod Marca", "Descripcion", "Precio", "Utilidad", "Cod Impuesto", "Costo", "Activo", "% Utlidad", "Existencia"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblarticulos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblarticulosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblarticulos);
        if (tblarticulos.getColumnModel().getColumnCount() > 0) {
            tblarticulos.getColumnModel().getColumn(0).setResizable(false);
            tblarticulos.getColumnModel().getColumn(1).setResizable(false);
            tblarticulos.getColumnModel().getColumn(2).setResizable(false);
            tblarticulos.getColumnModel().getColumn(3).setResizable(false);
            tblarticulos.getColumnModel().getColumn(4).setResizable(false);
            tblarticulos.getColumnModel().getColumn(5).setResizable(false);
            tblarticulos.getColumnModel().getColumn(6).setResizable(false);
            tblarticulos.getColumnModel().getColumn(7).setResizable(false);
            tblarticulos.getColumnModel().getColumn(8).setResizable(false);
        }

        jLabel8.setText("Cod Articulo");

        jLabel9.setText("Cod Familia");

        jLabel10.setText("Cod Marca");

        jLabel11.setText("Descripcion");

        jLabel12.setText("Precio n/a");

        jLabel13.setText("Cod Impuesto");

        jLabel14.setText("Costo");

        txtcosto.setEditable(false);
        txtcosto.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtcostoFocusLost(evt);
            }
        });

        btninsertar.setText("INSERTAR");
        btninsertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btninsertarActionPerformed(evt);
            }
        });

        btnmodificar.setText("MODIFICAR");
        btnmodificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnmodificarActionPerformed(evt);
            }
        });

        btneliminar.setText("ELIMINAR");
        btneliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneliminarActionPerformed(evt);
            }
        });

        btnfinalizarmodifi.setText("FINALIZAR");
        btnfinalizarmodifi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnfinalizarmodifiActionPerformed(evt);
            }
        });

        btnfinalizarinsertar.setText("FINALIZAR");
        btnfinalizarinsertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnfinalizarinsertarActionPerformed(evt);
            }
        });

        jButton6.setText("ATRAS");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel4.setText("Activo");

        rdSi.setText("SI");
        rdSi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdSiActionPerformed(evt);
            }
        });
        rdSi.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                rdSiFocusLost(evt);
            }
        });

        rdNo.setText("No");
        rdNo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdNoActionPerformed(evt);
            }
        });
        rdNo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                rdNoFocusLost(evt);
            }
        });

        cbxFamilias.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbxMarca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbxImpuesto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Utlidad");

        jlaexistencia.setText("Existecia");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btninsertar)
                        .addGap(18, 18, 18)
                        .addComponent(btnmodificar)
                        .addGap(18, 18, 18)
                        .addComponent(btneliminar)
                        .addGap(90, 90, 90)
                        .addComponent(btnfinalizarmodifi))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtcodarti)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(cbxFamilias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(cbxMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(txtdescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(txtpresinim, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlaexistencia)
                            .addComponent(txtExistencia, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(txtUtilidad, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cbxImpuesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtcosto, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnfinalizarinsertar)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jLabel1)
                        .addGap(35, 35, 35)
                        .addComponent(jLabel13)
                        .addGap(23, 23, 23)
                        .addComponent(jLabel14)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jLabel4)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(rdSi)
                        .addGap(18, 18, 18)
                        .addComponent(rdNo)
                        .addGap(0, 71, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jButton6))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12)
                            .addComponent(jlaexistencia)
                            .addComponent(jLabel1)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtcodarti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtdescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtpresinim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbxFamilias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbxMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtExistencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtUtilidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbxImpuesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtcosto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rdSi)
                            .addComponent(rdNo))
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btninsertar)
                            .addComponent(btnmodificar)
                            .addComponent(btneliminar)
                            .addComponent(btnfinalizarmodifi)
                            .addComponent(btnfinalizarinsertar))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton6))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // boton insertar
    private void btninsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btninsertarActionPerformed
        // TODO add your handling code here:
        limpiar();
        ver();
        btnfinalizarmodifi.setVisible(false);
        this.txtcodarti.requestFocus();
    }//GEN-LAST:event_btninsertarActionPerformed

    // finalizar el insert
    private void btnfinalizarinsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnfinalizarinsertarActionPerformed
        try {
            if (!(this.txtcodarti.getText().charAt(0) == ' ')) {
                try {
                    String si = "";
                    if (rdSi.isSelected()) {
                        si = "S";
                    } else {
                        if (rdNo.isSelected()) {
                            si = "N";
                        }
                    }

                    String codigoFam = "";
                    ResultSet datos2 = CP.select("cod_familia", "\"schUTN\".familia_articulo",
                            "descripcion =" + "'" + cbxFamilias.getSelectedItem() + "'");

                    while (datos2.next()) {

                        codigoFam = datos2.getString(1);

                    }

                    String codigoMrca = "";
                    ResultSet datos3 = CP.select("cod_marca", "\"schUTN\".marca_articulo",
                            "descripcion =" + "'" + cbxMarca.getSelectedItem() + "'");

                    while (datos3.next()) {

                        codigoMrca = datos3.getString(1);

                    }

                    String codigoImpuesto = "";
                    ResultSet datos4 = CP.select("cod_impuesto", "\"schUTN\".impuesto",
                            "descripcion =" + "'" + cbxImpuesto.getSelectedItem() + "'");

                    while (datos4.next()) {

                        codigoImpuesto = datos4.getString(1);

                    }
                    
                    double impuestoValor=0;
                    ResultSet datos5 = CP.select("porcentaje", "\"schUTN\".impuesto",
                            "descripcion =" + "'" + cbxImpuesto.getSelectedItem() + "'");

                    while (datos5.next()) {

                        impuestoValor =((Double.parseDouble(datos5.getString(1))/100));

                    }
                    double utilidad =0;
                    utilidad+= ((Double.parseDouble(this.txtUtilidad.getText())/100)*Double.parseDouble(this.txtpresinim.getText())+Double.parseDouble(this.txtpresinim.getText()));
                    
                    
                    
                    utilidad+=impuestoValor*(utilidad);
                    
                    utilidad= Math.round(utilidad);

                    boolean resultado = frmArticulo.CP.insert("\"schUTN\".articulo",
                            "("
                            + "'" + this.txtcodarti.getText() + "'" + ", "
                            + "'" + codigoFam + "', "
                            + "'" + codigoMrca + "',"
                            + "'" + this.txtdescripcion.getText() + "',"
                            + this.txtpresinim.getText() + ", "
                            + "'" + codigoImpuesto + "', "
                            + utilidad + ", "
                            + "'" + si+ "', "+
                                  this.txtUtilidad.getText()+
                                     ", "+txtExistencia.getText()+");");
                } catch (Exception e) {
                    System.out.printf("Error: " + e.toString());
                }
                limpiar();
                int validar = JOptionPane.showConfirmDialog(
                        null,
                        "Deseas Realizar Otra operacion",
                        "  ",
                        JOptionPane.YES_NO_OPTION);
                if (validar == JOptionPane.YES_OPTION) {
                    verdatos();
                    limpiar();
                } else {
                    verdatos();
                    noVer();
                }
                noVer();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Codigo Obligatorio");
        }
        limpiarRd();
    }//GEN-LAST:event_btnfinalizarinsertarActionPerformed

    // metodo limpiar los campos de los txt
    private void limpiar() {
        txtcodarti.setText("");

        txtExistencia.setText("");
        txtUtilidad.setText("");
        txtcosto.setText("");
        txtdescripcion.setText("");
        txtpresinim.setText("");
    }

    // finaliza la modificacion
    private void btnfinalizarmodifiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnfinalizarmodifiActionPerformed
        String cod;
        DefaultTableModel modelo = (DefaultTableModel) tblarticulos.getModel();
        int vector[] = tblarticulos.getSelectedRows();
        int in = tblarticulos.getSelectedRow();
        if (in == -1) {
            JOptionPane.showMessageDialog(null, "Selecione un valor valido");
        } else {
            for (int i = 0; i < vector.length; i++) {
                cod = (String) tblarticulos.getValueAt(in, 0);
                try {
                    String si = "";
                    if (rdSi.isSelected()) {
                        si = "S";
                    } else {
                        if (rdNo.isSelected()) {
                            si = "N";
                        }
                    }

                    String codigoFam = "";
                    ResultSet datos2 = CP.select("cod_familia", "\"schUTN\".familia_articulo",
                            "descripcion =" + "'" + cbxFamilias.getSelectedItem() + "'");

                    while (datos2.next()) {

                        codigoFam = datos2.getString(1);

                    }

                    String cofigoMarca = "";
                    ResultSet datos3 = CP.select("cod_marca", "\"schUTN\".marca_articulo",
                            "descripcion =" + "'" + cbxMarca.getSelectedItem() + "'");

                    while (datos3.next()) {

                        cofigoMarca = datos3.getString(1);

                    }

                    String codigoImpuesto = "";
                    ResultSet datos4 = CP.select("cod_impuesto", "\"schUTN\".impuesto",
                            "descripcion =" + "'" + cbxImpuesto.getSelectedItem() + "'");

                    while (datos4.next()) {

                        codigoImpuesto = datos4.getString(1);

                    }
                    
                    double impuestoValor=0;
                    ResultSet datos5 = CP.select("porcentaje", "\"schUTN\".impuesto",
                            "descripcion =" + "'" + cbxImpuesto.getSelectedItem() + "'");

                    while (datos5.next()) {

                        impuestoValor =((Double.parseDouble(datos5.getString(1))/100)+1);

                    }
                    double utilidad =0;
                    utilidad+= ((Double.parseDouble(this.txtUtilidad.getText())/100)*Double.parseDouble(this.txtpresinim.getText())+Double.parseDouble(this.txtpresinim.getText()));
                    
                    
                    
                    utilidad+=impuestoValor*(utilidad);
                    
                    
                    

                    boolean resultado = frmArticulo.CP.actualizar("\"schUTN\".articulo",
                            "cod_articulo= " + "'" + this.txtcodarti.getText() + "'" + ", "
                            + "cod_familia= " + "'" + codigoFam + "', "
                            + "cod_marca= " + "'" + cofigoMarca + "',"
                            + "descripcion= " + "'" + this.txtdescripcion.getText() + "',"
                            + "precio_sin_imp= " + this.txtpresinim.getText() + ", "
                            + "cod_impuesto= " + "'" + codigoImpuesto + "', "
                            + "costo= " + utilidad + ", "
                            + "activo= " + "'" + si + "', "
                            + "porcentaje_utilidad= "+this.txtUtilidad.getText()+", existencia="+txtExistencia.getText(), "cod_articulo= " + "'" + cod + "'");
                } catch (Exception e) {
                    System.out.printf("Error: " + e.toString());
                }
            }
        }
        verdatos();
        noVer();
        limpiarRd();
    }//GEN-LAST:event_btnfinalizarmodifiActionPerformed

    // boton modificar
    private void btnmodificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnmodificarActionPerformed
        ver();
        btnfinalizarinsertar.setVisible(false);
    }//GEN-LAST:event_btnmodificarActionPerformed

    // evento para seleccionar todos los campos de la tabla
    private void tblarticulosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblarticulosMouseClicked
        limpiarRd();
        String cod;
        DefaultTableModel modelo = (DefaultTableModel) tblarticulos.getModel();
        int vector[] = tblarticulos.getSelectedRows();
        int in = tblarticulos.getSelectedRow();

        cod = (String) tblarticulos.getValueAt(in, 0);
        txtcodarti.setText(cod);

//        cod = (String) tblarticulos.getValueAt(in, 1);
//        txtcodfa.setText(cod);
//        cod = (String) tblarticulos.getValueAt(in, 2);
//        txtcodmarca.setText(cod);
        cod = (String) tblarticulos.getValueAt(in, 3);
        txtdescripcion.setText(cod);

        cod = (String) tblarticulos.getValueAt(in, 4);
        txtpresinim.setText(cod);

        cod = (String) tblarticulos.getValueAt(in, 5);
        txtUtilidad.setText(cod);

        cod = (String) tblarticulos.getValueAt(in, 7);
        txtcosto.setText(cod);
        
       

        cod = (String) tblarticulos.getValueAt(in, 8);
        if (cod.equals("S")) {
            rdSi.setSelected(true);

        } else {
            rdNo.setSelected(true);
        }

        if (rdSi.isSelected()) {
            rdNo.setEnabled(false);
        } else {
            rdNo.setEnabled(true);
        }

        if (rdNo.isSelected()) {
            rdSi.setEnabled(false);

        } else {
            rdSi.setEnabled(true);
        }
        
//         cod = (String) tblarticulos.getValueAt(in, 8);
//        txtcosto.setText(cod);
        
        cod = (String) tblarticulos.getValueAt(in, 10);
        txtExistencia.setText(cod);
        
    }//GEN-LAST:event_tblarticulosMouseClicked

    // boton eliminar
    private void btneliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneliminarActionPerformed
        noVer();
        int validar = JOptionPane.showConfirmDialog(
                null,
                "Estas Seguro?",
                " CUIDADO",
                JOptionPane.YES_NO_OPTION);
        if (validar == JOptionPane.YES_OPTION) {
            try {
                boolean resultado = frmArticulo.CP.eliminar("\"schUTN\".articulo",
                        "cod_articulo= " + "'" + this.txtcodarti.getText() + "'");
            } catch (Exception e) {
                System.out.printf("Error: " + e.toString());
            }
            verdatos();
        } else {
            verdatos();
        }
        limpiarRd();
    }//GEN-LAST:event_btneliminarActionPerformed

    // boton atras para ventana principal
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        setVisible(false);
        frmPrincipal o = new frmPrincipal();
        o.setVisible(true);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void rdSiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdSiActionPerformed
        if (rdSi.isSelected()) {
            
            rdNo.setEnabled(false);
           
                
                
            
        
        } else {
            rdNo.setEnabled(true);
        }
    }//GEN-LAST:event_rdSiActionPerformed

    private void rdNoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdNoActionPerformed
        if (rdNo.isSelected()) {
            rdSi.setEnabled(false);
            
            

        } else {
            rdSi.setEnabled(true);
        }
    }//GEN-LAST:event_rdNoActionPerformed

    private void txtcostoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtcostoFocusLost
       
        if (rdSi.isSelected()==false && rdNo.isSelected()==false) {
            this.rdSi.requestFocus();
        }else{
            if (rdSi.isSelected()==true) {
                rdSi.requestFocus();
            }else{
                rdNo.requestFocus();
            }
        }
        
    }//GEN-LAST:event_txtcostoFocusLost

    private void rdSiFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_rdSiFocusLost
        if (rdSi.isSelected()==true) {
            rdNo.setEnabled(false);
                  if(this.btnfinalizarmodifi.isEnabled()==true)
           {
               this.btnfinalizarmodifi.requestFocus();
           }
          
           
               if (this.btnfinalizarinsertar.isEnabled()==true) {
                   
                   this.btnfinalizarinsertar.requestFocus();
               }
               
        } else {
            rdNo.setEnabled(true);
        }
    }//GEN-LAST:event_rdSiFocusLost

    private void rdNoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_rdNoFocusLost
       if (rdNo.isSelected()==true) {
            rdSi.setEnabled(false);
                  if(this.btnfinalizarmodifi.isEnabled()==true)
           {
               this.btnfinalizarmodifi.requestFocus();
           }
          
           
               if (this.btnfinalizarinsertar.isEnabled()==true) {
                   
                   this.btnfinalizarinsertar.requestFocus();
               }
               
        } else {
            rdSi.setEnabled(true);
        }
       
       if(rdNo.isSelected()==false&&rdSi.isSelected()==false)
       {
           this.rdSi.requestFocus();
       }
    }//GEN-LAST:event_rdNoFocusLost

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        // TODO add your handling code here:
          char tecla=evt.getKeyChar();
        if(tecla==KeyEvent.VK_ESCAPE)
        {
            setVisible(false);
            frmPrincipal F = new frmPrincipal();
            F.setVisible(true);
          
            
        }
    }//GEN-LAST:event_formKeyPressed

    //codigo generado por JAVA
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(() -> {
            try {
                new frmArticulo().setVisible(true);
            } catch (ClassNotFoundException | SQLException ex) {
                Logger.getLogger(frmArticulo.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btneliminar;
    private javax.swing.JButton btnfinalizarinsertar;
    private javax.swing.JButton btnfinalizarmodifi;
    private javax.swing.JButton btninsertar;
    private javax.swing.JButton btnmodificar;
    private javax.swing.JComboBox cbxFamilias;
    private javax.swing.JComboBox cbxImpuesto;
    private javax.swing.JComboBox cbxMarca;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jlaexistencia;
    private javax.swing.JRadioButton rdNo;
    private javax.swing.JRadioButton rdSi;
    private javax.swing.JTable tblarticulos;
    private javax.swing.JTextField txtExistencia;
    private javax.swing.JTextField txtUtilidad;
    private javax.swing.JTextField txtcodarti;
    private javax.swing.JTextField txtcosto;
    private javax.swing.JTextField txtdescripcion;
    private javax.swing.JTextField txtpresinim;
    // End of variables declaration//GEN-END:variables
}
